package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.AccountRoleMapper;
import com.board.dao.AdminInfoMapper;
import com.board.dao.RoleMapper;
import com.board.dto.AccountRoleDto;
import com.board.entity.AccountRole;
import com.board.entity.AdminInfo;
import com.board.entity.Role;
import com.board.exception.AccountRoleException;
import com.board.form.UpdateAccountRoleForm;
import com.board.param.AccountRoleParam;
import com.board.service.AccountRoleService;
import com.board.util.PageInfo;
import com.board.util.StateDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月03日
 */
@Service
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements AccountRoleService {
    @Autowired
    private AccountRoleMapper accountRoleMapper;
    @Autowired
    private AdminInfoMapper adminInfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public PageInfo<AccountRoleDto> loadAccountRole(AccountRoleParam param) {
        //先从账号表查询出分页后的账号信息
        Page<AdminInfo> page = new Page<>(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?5:param.getPageSize());
        QueryWrapper<AdminInfo> wrapper = new QueryWrapper<>();
        if (param.getAccountName()!=null&&param.getAccountName()!=""){
            wrapper.like("admin_user",param.getAccountName());
        }
        adminInfoMapper.selectPage(page,wrapper);
        List<AccountRoleDto> dtos = BeanUtil.copyToList(page.getRecords(), AccountRoleDto.class);
        List<Integer> ids = dtos.stream().map(AccountRoleDto::getAdminId).collect(Collectors.toList());
        //再从账号角色表查询出账号对应的角色信息
        List<AccountRole> roles = accountRoleMapper.selectList(new QueryWrapper<AccountRole>().in("account_id", ids));
        List<Integer> roleids = roles.stream().map(accountRole -> accountRole.getRoleId()).collect(Collectors.toList());
        //从角色表查询角色名称
        List<Role> roleList = roleMapper.selectList(new QueryWrapper<Role>().in("role_id", roleids));
        HashMap<Integer, String> roleMap = new HashMap<>();
        for (Role role : roleList) {
            roleMap.put(role.getRoleId(),role.getRoleName());
        }
        HashMap<Integer, Integer> accountRoleMap = new HashMap<>();
        for (AccountRole role : roles) {
            accountRoleMap.put(role.getAccountId(),role.getRoleId());
        }
        for (AccountRoleDto dto : dtos) {
            dto.setRoleId(accountRoleMap.get(dto.getAdminId()));
            dto.setRoleName(roleMap.get(dto.getRoleId()));
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(dtos);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }

    @Override
    public AccountRoleDto accountToRole(UpdateAccountRoleForm form) {
        //先查询账号和角色是否存在
        AdminInfo adminInfo = adminInfoMapper.selectById(form.getAccountId());
        if (adminInfo==null) throw new AccountRoleException(500,"账号不存在");
        Role role = roleMapper.selectById(form.getRoleId());
        if (role==null) throw new AccountRoleException(500,"角色不存在");
        //账号状态是否正常
        if (adminInfo.getAdminState()!= StateDate.ADMIN_STATE_NORMAL){
            throw new AccountRoleException(500,"账号状态异常");
        }
        //修改账号所属角色，先删除原来对应的数据
        accountRoleMapper.delete(new QueryWrapper<AccountRole>().eq("account_id",form.getAccountId()));
        AccountRole accountRole = new AccountRole();
        accountRole.setAccountId(form.getAccountId());
        accountRole.setRoleId(form.getRoleId());
        accountRoleMapper.insert(accountRole);
        AccountRoleDto dto = BeanUtil.toBean(adminInfo, AccountRoleDto.class);
        dto.setRoleId(form.getRoleId());
        dto.setRoleName(role.getRoleName());
        return dto;
    }
}
